﻿#!/usr/bin/python
# -*- coding: utf-8 -*-
#
# Copyright (C) 2008 ZHENG Zhong <heavyzheng nospam-at gmail D0T com>
# - http://heavyz.blogspot.com/
# - http://buggarden.blogspot.com/
#
# Created on 2008-05-06.
# $Id: views.py 307 2008-12-29 13:48:00Z heavyzheng $
#

import logging

from django.template import loader
from django.template import Context
from django.http import HttpRequest
from django.http import HttpResponse
from django.http import HttpResponseRedirect

from google.appengine.api import users

#---------------------------------------------------------------------------------------------------
# Bug garden homepage
#---------------------------------------------------------------------------------------------------

def _buggarden_home(request):
    user = users.get_current_user()
    login_url = users.create_login_url(request.path)
    logout_url = users.create_logout_url(request.path)
    ctxt_dict = { "user_" : user,
                  "login_url_" : login_url,
                  "logout_url_" : logout_url,
                  "static_" : "/static", }
    ctxt = Context(ctxt_dict)
    tmpl = loader.get_template("buggarden.html")
    return HttpResponse(tmpl.render(ctxt))

#---------------------------------------------------------------------------------------------------
# Sub-webapp homepages
#---------------------------------------------------------------------------------------------------

def _fnr_home(request):
    return HttpResponseRedirect("/fnr/")

def _afcp_home(request):
    return HttpResponseRedirect("/afcp/")

def _default_home(request):
    return _buggarden_home(request)


HOME_HANDLER_MAPPINGS = { "buggarden" : _buggarden_home, "fnr" : _fnr_home, "afcp" : _afcp_home, }

SERVER_HANDLER_MAPPINGS = { "buggarden.zhengzhong.info" : _buggarden_home,
                            "buggarden.zhengzhong.net"  : _buggarden_home,
                            "bug-garden.appspot.com"    : _buggarden_home,
                            "www.foodsnroses.com"       : _fnr_home,
                            "foodsnroses.appspot.com"   : _fnr_home,
                            "www.afcp-paristech.org"    : _afcp_home,
                            "beta.afcp-paristech.org"   : _afcp_home,
                            "afcp.appspot.com"          : _afcp_home, }

#---------------------------------------------------------------------------------------------------
# Homepage handler
#---------------------------------------------------------------------------------------------------

def home(request):
    home = request.REQUEST.get("home")
    handler = HOME_HANDLER_MAPPINGS.get(home, None)
    if handler is not None:
        return handler(request)
    else:
        server = request.META.get("SERVER_NAME")
        handler = SERVER_HANDLER_MAPPINGS.get(server, None)
        if handler is not None:
            return handler(request)
        else:
            return _default_home(request)

#---------------------------------------------------------------------------------------------------
# The 404 not-found page
#---------------------------------------------------------------------------------------------------

def not_found(request):
    """Renders the 404 not-found page.
    """
    
    logging.info("The requested page '%s' does not exist." % request.path)
    ctxt_dict = { "url": request.path, }
    ctxt = Context(ctxt_dict)
    tmpl = loader.get_template("not_found.html")
    return HttpResponse(tmpl.render(ctxt))

#---------------------------------------------------------------------------------------------------
# Sandbox for testing new features
#---------------------------------------------------------------------------------------------------

def sandbox(request):

    ctxt_dict = { }
    
    # Set language.
    request.session["django_language"] = "fr"
    
    # Session.
    value = request.session.get("value", 0)
    value += 1
    request.session["value"] = value
    ctxt_dict["value"] = value
    
    # More testing code goes here...
    
    ctxt = Context(ctxt_dict)
    tmpl = loader.get_template("sandbox.html")
    resp = HttpResponse(tmpl.render(ctxt))
    resp.set_cookie(key="greeting", value="hello, world")
    return resp



